import { Router } from 'express';
import organizationController from '@controllers/organization.controller';
import { authenticate } from '@middleware/auth';
import {
  requireOrganizationAdmin,
  requireOrganizationOwner,
  requireOrganizationMember,
  organizationsPermission
} from '@middleware/permission';

const router = Router();

/**
 * @route   POST /api/organizations
 * @desc    Create a new organization
 * @access  Authenticated
 */
router.post(
  '/',
  authenticate,
  organizationController.create
);

/**
 * @route   GET /api/organizations
 * @desc    Get all organizations (with pagination)
 * @access  Authenticated
 */
router.get(
  '/',
  authenticate,
  organizationController.getAll
);

/**
 * @route   GET /api/organizations/my
 * @desc    Get current user's organizations
 * @access  Authenticated
 */
router.get(
  '/my',
  authenticate,
  organizationController.getUserOrganizations
);

/**
 * @route   GET /api/organizations/:id
 * @desc    Get organization by ID
 * @access  Organization Member
 */
router.get(
  '/:id',
  authenticate,
  requireOrganizationMember,
  organizationController.getById
);

/**
 * @route   PUT /api/organizations/:id
 * @desc    Update organization
 * @access  Organization Admin
 */
router.put(
  '/:id',
  authenticate,
  requireOrganizationAdmin,
  organizationController.update
);

/**
 * @route   DELETE /api/organizations/:id
 * @desc    Delete organization (soft delete)
 * @access  Organization Owner
 */
router.delete(
  '/:id',
  authenticate,
  requireOrganizationOwner,
  organizationController.delete
);

/**
 * @route   GET /api/organizations/:id/members
 * @desc    Get organization members
 * @access  Organization Member
 */
router.get(
  '/:id/members',
  authenticate,
  requireOrganizationMember,
  organizationController.getMembers
);

/**
 * @route   POST /api/organizations/:id/members
 * @desc    Add member to organization
 * @access  Organization Admin
 */
router.post(
  '/:id/members',
  authenticate,
  requireOrganizationAdmin,
  organizationController.addMember
);

/**
 * @route   DELETE /api/organizations/:id/members/:userId
 * @desc    Remove member from organization
 * @access  Organization Admin
 */
router.delete(
  '/:id/members/:userId',
  authenticate,
  requireOrganizationAdmin,
  organizationController.removeMember
);

export default router;
